/*
 * FeedService.java
 * Copyright (c) 2014, 2019, Kiri. 
 * All rights reserved.
 */
package com.onesalt.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.onesalt.domain.Dinner;
import com.onesalt.domain.Feed;
import com.onesalt.domain.Kitchen;

/**
 * 
 *
 * @author kiri
 * @date 2015年4月19日
 * @version 1.0
 */
@Service
public class FeedService extends AbstractService {

	private final FeedRepository feedRepository;
	private final UserRepository userRepository;
	private final TopicRepository topicRepository;
	private final AdsRepository adsRepository;
	private final KitchenRepository kitchenRepository;
	private final DinnerRepository dinnerRepository;

	@Autowired
	public FeedService(FeedRepository feedRepository, UserRepository userRepository, TopicRepository topicRepository,
			AdsRepository adsRepository, KitchenRepository kitchenRepository, DinnerRepository dinnerRepository) {
		this.feedRepository = feedRepository;
		this.userRepository = userRepository;
		this.topicRepository = topicRepository;
		this.adsRepository = adsRepository;
		this.kitchenRepository = kitchenRepository;
		this.dinnerRepository = dinnerRepository;
	}

	public FeedRepository getFeedRepository() {
		return feedRepository;
	}
	
	public UserRepository getUserRepository() {
		return userRepository;
	}

	public TopicRepository getTopicRepository() {
		return topicRepository;
	}

	public AdsRepository getAdsRepository() {
		return adsRepository;
	}

	public KitchenRepository getKitchenRepository() {
		return kitchenRepository;
	}

	public DinnerRepository getDinnerRepository() {
		return dinnerRepository;
	}


	public Page<Feed> findAll(int page, int size) {
		return getFeedRepository().findAll(new PageRequest(page, size, Direction.DESC, "prority", "createTime"));
	}

	public void fillFeedItems(List<Feed> feeds) {
		for (Feed feed : feeds) {
			fillFeedItems(feed);
		}
	}
	
	public void fillFeedItems(Feed feed) {
		switch (feed.getType()) {
		case Feed.TYPE_AD:
		{
			feed.setAds(getAdsRepository().findOne(feed.getAdsId()));
			break;
		}
		case Feed.TYPE_TOPIC:
		{
			feed.setTopic(getTopicRepository().findOne(feed.getTopicId()));
			break;
		}
		case Feed.TYPE_KITCHEN:
		{
			Kitchen kitchen = getKitchenRepository().findOne(feed.getKitchenId());
			kitchen.setCook(getUserRepository().findOne(kitchen.getCookId()));
			feed.setKitchen(kitchen);;
			break;
		}
		case Feed.TYPE_DINNER:
		{
			Dinner dinner = getDinnerRepository().findOne(feed.getDinnerId());
			Kitchen kitchen = getKitchenRepository().findOne(dinner.getKitchenId());
			kitchen.setCook(getUserRepository().findOne(kitchen.getCookId()));
			feed.setDinner(dinner);;
			break;
		}
		default:
			break;
		}
	}

}
